package com.deluxelx.rmo.essay.controller;

import cn.hutool.core.util.ObjectUtil;
import com.deluxelx.rmo.essay.api.vo.EssayVo;
import com.deluxelx.rmo.essay.api.res.EssayRes;
import com.deluxelx.rmo.essay.api.EssayService;
import com.deluxelx.rmo.user.api.rpc.UserSentimentService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

import static com.deluxelx.rmo.common.constants.TreeConstants.SENTIMENT_ESSAY_TREE;

@RestController
@RequestMapping("/essay")
public class EssayController {

    @DubboReference
    private UserSentimentService userSentimentService;

    @Resource
    private EssayService essayService;

    /**
     * 根据用户的特性，针对性的为用户推荐相应文章
     * @param userId 用户 Id
     * @return 经过服务器推荐算法的推荐的文章
     */
    @GetMapping("/recommend")
    public EssayRes<List<EssayVo>> recommend(@RequestParam(required = false) Long userId) {
        return (ObjectUtil.isNull(userId))
                ? essayService.defaultRecommendEssay()
                : essayService.recommendEssayBySentiment(SENTIMENT_ESSAY_TREE, userSentimentService.getUserSentiment(userId));
    }

}
